package com.bdqn.desiremall.serivce.user;

import org.apache.ibatis.session.SqlSession;

import com.bdqn.desiremall.entity.Mall_User;
import com.bdqn.desiremall.mapper.Mall_UserMapper;
import com.desiremall.util.MyBatisUtil;

public class UserServiceImpl implements UserService {
	int i;
	SqlSession session = null;
	Mall_User user = null;

	@Override
	public Mall_User login(String userName, String password) {
		try {
			// 1����ȡsqlSession����
			session = MyBatisUtil.getSession();
			// 2����ȡUserMapperʵ��
			Mall_UserMapper userMapper = session
					.getMapper(Mall_UserMapper.class);

			user = userMapper.findUse(userName, password);
			return user;
		} catch (Exception e) {
			// session.rollback();
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return user;
	}

	@Override
	public boolean register(Mall_User user) {
		try {
			// 1����ȡsqlSession����
			session = MyBatisUtil.getSession();
			// 2����ȡUserMapperʵ��
			Mall_UserMapper userMapper = session
					.getMapper(Mall_UserMapper.class);
			// 3�����i����һ��˵����ӳɹ�������true
			i = userMapper.addUser(user);
			if (0 < i) {
				session.commit();
				return true;
			}
		} catch (Exception e) {
			session.rollback();
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return false;
	}

	@Override
	public boolean forgetPassword(String password, String userMobileCode) {

		return false;
	}

	@Override
	public boolean changeUserInfo(Mall_User user) {
		try {
			// 1����ȡsqlSession����
			session = MyBatisUtil.getSession();
			// 2����ȡUserMapperʵ��
			Mall_UserMapper userMapper = session
					.getMapper(Mall_UserMapper.class);
			// 3�����i����һ��˵���޸ĳɹ�������true
			i = userMapper.modifyUserInfo(user);

			if (0 < i) {
				session.commit();
				return true;
			}
		} catch (Exception e) {
			session.rollback();
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return false;
	}

	@Override
	public boolean isExist(Mall_User mall_User) {
		try {
			session = MyBatisUtil.getSession();
			Mall_UserMapper userMapper = session
					.getMapper(Mall_UserMapper.class);
			user = userMapper.isExist(mall_User);
			if (user != null) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return false;
	}

}
